package com.test.ch08;

import java.util.Comparator;
import java.util.Objects;

public class Student implements Comparable<Student> {
    private String id;
    private String name;
    private int age;
    private double score;
    //无参和有参构造器。Setter/Getter,toString
    //equals hashCode
    public Student() {
    }

    public Student(String id, String name, int age, double score) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return Objects.equals(id, student.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public int compareTo(Student o) {
        //this-o正序
        int result=(int)(o.getScore()-this.score);
        if(result==0){//成绩
            result=o.getName().compareTo(this.name);
            if(result==0){//姓名
                result=this.getAge()-o.age;
                if(result==0)//年龄
                {
                    result=this.id.compareTo(o.getId());
                }

            }
        }
        return 0;
    }
}
